/**
 * 不同环境不同配置文件
 */
'use strict';
(function () {
    /**
     * 除release环境外的其他环境只需配置差异域名，如域名与release环境一致，则不用配置
     */
    var config = {
        //生产环境
        release: {
            qmyx: 'http://qmyxcg.myscrm.com.cn',//全名营销
            ydxs: 'http://ydxsbg.myscrm.com.cn',//移动销售
            wls: 'http://wlsadmin.myscrm.com.cn',//微楼书
            wls_front: 'http://wls.myscrm.cn',//微楼书前台
            ztcsh: 'http://ztcsh.myscrm.com.cn',//新房秒杀
            mysrc: 'http://www.myscrm.com.cn',//旧版域名
            wx: 'http://wx.myscrm.cn',//微信
            opcenter: 'http://help.myscrm.com.cn',//运营中心
            yykf: 'http://yykf.myscrm.cn',//预约看房微信端
            rclc: 'http://rclc.myscrm.cn',//认筹理财微信端
            pigcms: 'http://www.myscrm.cn', //小猪
            ex_monitor: 'http://monitor.myscrm.cn',//错误监控
            yunke_back: 'http://rent.myscrm.cn',//明源后台
            yunlian_contract:'http://ylht.mingyuanyun.com'
        },
        //开发环境
        dev: {
            qmyx: 'http://dev-qmyx-cg.myscrm.cn:1002',
            ydxs: 'http://dev-ydxs-cg.myscrm.cn:1002',
            wls: 'http://dev-wls-cg.myscrm.cn:1002',
            wls_front: 'http://10.5.7.138:9219',
            ztcsh: 'http://middlend.myscrm.cn:1002',
            wx: 'http://wx-test.myscrm.cn',
            mysrc: 'http://dev-wcg.myscrm.cn:1001',
            opcenter: 'http://dev-help.myscrm.cn:9073',
            yykf: 'http://dev-appointment.myscrm.cn:1004',
            rclc: 'http://dev-rclc.myscrm.cn:1004',
            pigcms: 'http://www.myscrm.cn',
            ex_monitor: 'http://monitor-test.myscrm.cn',
            yunke_back: 'http://dev-rent.myscrm.cn:1002'//明源后台
        },
        //测试环境
        test: {
            qmyx: 'http://test-qmyxcg.myscrm.com.cn',
            ydxs: 'http://test-ydxsbg.myscrm.com.cn',
            wls: 'http://testwls.myscrm.com.cn',
            wls_front: 'http://testwls.myscrm.cn',
            ztcsh: 'http://ztcshtest.myscrm.com.cn',
            wx: 'http://wx-test.myscrm.cn',
            mysrc: 'http://func-manage.myscrm.com.cn',
            opcenter: 'http://test-help.myscrm.com.cn',
            yykf: 'http://yykf-test.myscrm.cn',
            rclc: 'http://rclc-test.myscrm.cn',
            pigcms: 'http://pigcms-test.myscrm.cn', 
            ex_monitor: 'http://monitor-test.myscrm.cn',
            yunke_back: 'http://funcsz.myscrm.cn:8088'
        },
        //预发布环境
        pre: {
            qmyx: 'http://beta-qmyx-cg.myscrm.cn',
            ydxs: 'http://beta-ydxs-cg.myscrm.cn',
            wls: 'http://testwx.myscrm.cn:8013',
            wls_front: 'http://testwx.myscrm.cn:8014',
            ztcsh: 'http://ztcshbeta.myscrm.cn',
            wx: 'http://wx-release.myscrm.cn',
            mysrc: 'http://beta-wcg.myscrm.cn',
            yykf: 'http://yykf-beta.myscrm.cn',
            rclc: 'http://rclc-beta.myscrm.cn',
            pigcms: 'http://beta-pig-cg.myscrm.cn', 
            ex_monitor: 'http://monitor-release.myscrm.cn',
            yunke_back: 'http://beta.myscrm.cn:9088'
        },
        //体验环境
        demo: {
            qmyx: 'http://demo-qmyxcg.myscrm.com.cn',
            ydxs: 'http://demo-ydxsbg.myscrm.com.cn',
            wls: 'http://demo-wlsadmin.myscrm.com.cn',
            wls_front: 'http://demo-wls.myscrm.cn',
            ztcsh: 'ztcshdemo.myscrm.com.cn',
            wx: 'http://demo-wx.myscrm.cn',
            mysrc: 'http://demo-manage.myscrm.com.cn',
            yykf: 'http://demo-yykf.myscrm.cn',
            pigcms: 'http://demo-pigcms.myscrm.cn', 
            rclc:'http://demo-rclc.myscrm.cn'
        },
        //代理版预发布环境
        agentPre: {
            qmyx: 'http://beta-daili-qmyx-cg.myscrm.cn',
            ydxs: 'http://beta-daili-ydxs-cg.myscrm.cn',
            wls: 'http://daili-testwx.myscrm.cn',
            wls_front: 'http://daili-testwx.myscrm.cn:8015',
            mysrc: 'http://daili-manage.myscrm.cn',
            yykf: 'http://yykf-beta-daili.myscrm.cn',
            rclc: 'http://rclc-beta-daili.myscrm.cn',
            pigcms: 'http://beta-daili-pig-cg.myscrm.cn',
            ex_monitor: 'http://monitor-release.myscrm.cn'
        },
        //代理版生产环境
        agentProduct: {
            qmyx: 'http://qmyxcg-daili.myscrm.com.cn',
            ydxs: 'http://daili-ydxsbg.myscrm.com.cn',
            wls: 'http://wlsadmin-daili.myscrm.com.cn',
            wls_front: 'http://wls-daili.myscrm.cn',
            mysrc: 'http://daili.myscrm.com.cn',
            yykf: 'http://yykf-daili.myscrm.cn',
            rclc: 'http://rclc-daili.myscrm.cn',
            pigcms: 'http://www.myscrm.cn', 
            ex_monitor: 'http://monitor.myscrm.cn'
        }
    };

    var releaseConfig = config.release;

    var siteIds = [];
    for (var siteId in releaseConfig) {
        if (siteId && releaseConfig.hasOwnProperty(siteId)) {
            siteIds.push(siteId);
        }
    }

    //将各环境下未配置的站点域名使用release配置填充
    for (var envId in config) {
        if (envId && config.hasOwnProperty(envId)) {
            var envMap = config[envId];

            var siteId;
            for (var i = 0; siteId = siteIds[i]; i++) {
                if (!envMap[siteId]) {
                    envMap[siteId] = releaseConfig[siteId];
                }
            }
        }
    }

    var currentHost = location.protocol + '//' + location.host;
    var currentHostname = location.hostname;


    var hostnameReg = /http[s]?:\/\/([^:\/]+)/;

    function getHostName(url) {
        var match = url.match(hostnameReg);
        if (match) {
            return match[1];
        }
        return null;
    }

    var currentEnvMap;

    //查找当前域名所在环境
    labelForOuter : for (var envId in config) {
        if (envId && config.hasOwnProperty(envId)) {
            var envMap = config[envId];

            for (var siteId in envMap) {
                if (siteId && envMap.hasOwnProperty(siteId)) {
                    var url = envMap[siteId];
                    if (url === currentHost || getHostName(url) === currentHostname) {
                        currentEnvMap = envMap;
                        break labelForOuter;
                    }
                }
            }
        }
    }

    if (!currentEnvMap) {
        currentEnvMap = releaseConfig;
        console.warn('无法识别的环境域名，请先联系开发人员配置环境域名！');
    }

    window['ENVIRONMENT_YUNKE'] = currentEnvMap;
})();

if (typeof define == 'function') {
    define(function (require) {
        return window['ENVIRONMENT_YUNKE'];
    });
}
